<?php

namespace App\Models\goods;

use App\Common\statement\CommonStatement;
use App\Models\base\traits\HasOwner;
use App\Models\goods\observe\OrderObserve;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;





/**
 * App\Models\goods\Order
 *
 * @property int $id
 * @property string|null $order_no 订单编号
 * @property int $user_id 用户ID
 * @property float $total_price 总价
 * @property string|null $reason 退款时填写的退款理由
 * @property float $money 余额支付金额
 * @property float $pay_money 第三方支付金额
 * @property string|null $pay_info 支付说明
 * @property int $status 订单状态 -1 => 订单已取消  0=>代付款  1=>已付款(代发货)  2 => 待收货(已发货)  3 => 已收货(完成订单)  4 => 退款中  5=>已退款
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $out_trade_no 第三方支付订单号
 * @property string|null $nu 物流单号
 * @property string|null $nu_code 快递公司编号
 * @property int $order_group_id 关联拼团ID  不为0即加入了拼团
 * @property string $detail 地址详情
 * @property string $phone 联系电话
 * @property string $user_name 联系人名
 * @property string|null $deleted_at
 * @property float $rebate_money 返点总金额
 * @property string|null $remark 订单备注
 * @property float $old_price 原价
 * @property float $postage 邮费
 * @property string|null $pay_at 支付时间
 * @property string|null $send_at 发货时间
 * @property string|null $finish_at 完成时间
 * @property int $is_evaluate 是否已评价完成
 * @property int $is_safe 是否购买包邮保险  1是0否  是多出1元
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\goods\OrderDetail[] $details
 * @property-read int|null $details_count
 * @property-read mixed $nu_text
 * @property-read mixed $status_text
 * @property-read \App\Models\User $user
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Order onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order owner($userId)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereDetail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereFinishAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereIsEvaluate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereIsSafe($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereNu($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereNuCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereOldPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereOrderGroupId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereOrderNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereOutTradeNo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order wherePayAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order wherePayInfo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order wherePayMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order wherePostage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereRebateMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereSendAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereTotalPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereUserName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Order withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\goods\Order withoutTrashed()
 * @mixin \Eloquent
 * @property int $is_sign_for 是否扫码签收
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\goods\Order whereIsSignFor($value)
 */
class Order extends Model
{


    use HasOwner;
    use SoftDeletes;

    protected $fillable = [
        'order_no',
        'user_id',
        'total_price',
        'detail',
        'money',
        'pay_money',
        'pay_info',
        'status',
        'detail',
        'phone',
        'user_name',
        'remark',
        'old_price',
        'order_group_id',
        'postage',
        'is_safe'
    ];

    public static function boot()
    {
        parent::boot();
        static::observe(new OrderObserve());

    }

    public function details()
    {
        return $this->hasMany(OrderDetail::class);
    }


    protected $appends = [
        'status_text',
    ];

//    public function getUserAttribute()
//    {
//        if ($this->user_id != 0){
//            return User::select(['id','name','header_img'])->find($this->user_id);
//        }
//
//        return null;
//    }

    public function getStatusTextAttribute()
    {
        return CommonStatement::order_status[$this->status];
    }

    public function getNuTextAttribute()
    {
        return getExpresses()[$this->nu_code]??'';
    }


}
